package com.hzsxy.bighomework.serviceImpl;

import com.hzsxy.bighomework.api.HomeworkView;
import com.hzsxy.bighomework.api.QuestionView;
import com.hzsxy.bighomework.entity.*;
import com.hzsxy.bighomework.repository.*;
import com.hzsxy.bighomework.service.QuestionService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private StudentClassRepository studentClassRepository;
    @Autowired
    private ChoiceRepository choiceRepository;
    @Autowired
    private DesignRepository designRepository;
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private HomeworkRepository homeworkRepository;
    @Autowired
    private SubmitRepository submitRepository;

    @Override
    public List<QuestionView> getAllQuestion(String type,String author) {

        List<QuestionDO> questions;
        if ("admin".equals(type)){
            questions = (List<QuestionDO>) questionRepository.findAll();
        }else {
            questions = questionRepository.findByAuthor(author);
        }
        return questions.stream().map(this::assemble).collect(Collectors.toList());
    }

    @Override
    public void saveChoice(int questionId, String choiceInfo, String op1, String op2, String op3, String op4) {
        ChoiceDO choiceDO = new ChoiceDO(questionId,choiceInfo,op1,op2,op3,op4);
        choiceRepository.save(choiceDO);
    }

    @Override
    public void updateChoice(int choiceId, int questionId, String choiceInfo, String op1, String op2, String op3, String op4) {
        ChoiceDO choiceDO = new ChoiceDO(choiceId,questionId,choiceInfo,op1,op2,op3,op4);
        choiceRepository.save(choiceDO);
    }

    @Override
    public void saveDesign(int questionId, String designInfo) {
        DesignDO designDO = new DesignDO();
        designDO.setQuestionId(questionId);
        designDO.setDesignInfo(designInfo);
        designRepository.save(designDO);
    }

    @Override
    public List<HomeworkView> getHomework(String studentId) {
        List<StudentClassDO> studentClassDOList = studentClassRepository.findByStudentId(studentId);
        List<HomeworkView> viewList = new ArrayList<>();
        studentClassDOList.forEach( it -> {
            List<HomeworkDO> homeworkDOList = homeworkRepository.findByClassId(it.getClassId());
            List<HomeworkView> homeworkViewList = homeworkDOList.stream().filter(homework-> homework.getPublishTime().getTime()<new Date().getTime())
                    .map(homework -> {
                HomeworkView view = new HomeworkView(homework.getId(),homework.getName(),homework.getPublishTime().toString(),
                        homework.getEndTime().toString(),homework.getQuestionId());
                if(CollectionUtils.isEmpty(submitRepository.findByHomeworkIdAndStudentId(homework.getId(),studentId))){
                    view.setFinished(false);
                }else {
                    view.setFinished(true);
                }
                view.setTimeOut(new Date().getTime() > homework.getEndTime().getTime());
                return view;
            }).collect(Collectors.toList());
            viewList.addAll(homeworkViewList);
        });
        return viewList;
    }


    private QuestionView assemble(QuestionDO questionDO){
        QuestionView view = new QuestionView();
        view.setAuthor(questionDO.getAuthor());
        view.setDescription(questionDO.getDescription());
        view.setQuestionId(questionDO.getQuestionId());
        view.setTitle(questionDO.getTitle());
        return view;
    }
}
